package com.ch999.detect.View.dialog;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.ch999.detect.R;
import com.ch999.detect.View.SelectStartDialog;
import com.ch999.detect.mode.bean.GoodsBjgz;

import java.util.List;


/**
 * 自定义通用dialog
 * Created by Administrator on 2016/12/13 0013.
 */

public class CustomSelectDialog extends SelectStartDialog {

    private final Context mContext;
    private TextView tvTitle;
    private TextView tvOk;
    private TextView tvCountDown;//倒计时

    private ListView listview;//自定义view
    private MyAdapter mAdapter;

    public CustomSelectDialog(Context context) {
        super(context);
        mContext = context;
    }

    /**
     * Interface definition for a callback to be invoked when an item in this
     * AdapterView has been clicked.
     */
    public interface OnItemClickListener {

        void onItemClick(GoodsBjgz.AttrBean.ValuesBean valuesBean, int position);
    }

    OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_select_custom);


        tvTitle = (TextView) findViewById(R.id.tvTitle);

        listview = (ListView) findViewById(R.id.listview);
        mAdapter = new MyAdapter();
        listview.setAdapter(mAdapter);

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mOnItemClickListener != null) {
                    dismiss();
                    mOnItemClickListener.onItemClick(mAdapter.getItem(position), position);
                }
            }
        });


    }

    public class MyAdapter extends BaseAdapter{

        List<GoodsBjgz.AttrBean.ValuesBean> data;
        @Override
        public int getCount() {
            if (data == null) {
                return 0;
            }
            return data.size();
        }

        public void setData(List<GoodsBjgz.AttrBean.ValuesBean> data) {
            this.data = data;

            notifyDataSetChanged();
        }

        @Override
        public GoodsBjgz.AttrBean.ValuesBean getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            GoodsBjgz.AttrBean.ValuesBean valuesBean = data.get(position);
            View inflate = View.inflate(mContext, R.layout.item_select_text_layout, null);
            TextView viewById = (TextView) inflate.findViewById(R.id.tv_name_value);
            viewById.setText(valuesBean.getName());

            return inflate;
        }
    }


    public void show(List<GoodsBjgz.AttrBean.ValuesBean> data, String name) {
        super.show();
        mAdapter.setData(data);

        tvTitle.setText(name);
    }

    public void setTvTitle(String title) {
        if (tvTitle != null) {
            tvTitle.setText(title);
        }
    }



}
